//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.huashi.otg.sdk;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;

import com.example.libdecodewlt.RAW2BMP;
import com.hww.base.util.FileIOUtils;

public class Utility {
	private static final String TAG = "Utility";
	static final String[] nation = new String[] { "汉", "蒙古", "回", "藏", "维吾尔", "苗", "彝", "壮", "布依", "朝鲜", "满", "侗", "瑶",
			"白", "土家", "哈尼", "哈萨克", "傣", "黎", "傈僳", "佤", "畲", "高山", "拉祜", "水", "东乡", "纳西", "景颇", "克尔克孜", "土", "达斡尔",
			"仫佬", "羌", "布朗", "撒拉", "毛南", "仡佬", "锡伯", "阿昌", "普米", "塔吉克", "怒", "乌兹别克", "俄罗斯", "鄂温克", "德昂", "保安", "裕固",
			"京", "塔塔尔", "独龙", "鄂伦春", "赫哲", "门巴", "珞巴", "基诺", "穿青衣", "其他", "外国血统中国籍人士" };

	public Utility() {
	}

	public static void PersonInfoUtoG(byte[] cCardData, HSIDCardInfo ici) throws Exception {
		byte[] newmsg = new byte[cCardData.length + 2];
		newmsg[0] = -1;
		newmsg[1] = -2;

		for (int str = 0; str < cCardData.length; ++str) {
			newmsg[str + 2] = cCardData[str];
		}

		String var23 = new String(cCardData, "UTF-16LE");
		if (cCardData[248] == 73) {
			ici.setcertType("I");
			PersonInfoUtoGForeigners(cCardData, ici);
		} else if (cCardData[248] == 74) {
			ici.setcertType("J");
			PersonInfoUtoGHK(cCardData, ici);
		} else {
			ici.setcertType(" ");
			String Name = var23.substring(0, 15).replace("\u0000", "").trim();
			ici.setPeopleName(Name);
			String Sex = var23.substring(15, 16).replace("\u0000", "").trim();
			ici.setSex(Sex.equals("1") ? "男" : "女");
			String people = var23.substring(16, 18).replace("\u0000", "").trim();
			people = nation[Integer.parseInt(people) - 1];
			ici.setPeople(people);
			int birthYeah = Integer.parseInt(var23.substring(18, 22).replace("\u0000", "").trim());
			int birthMonth = Integer.parseInt(var23.substring(22, 24).replace("\u0000", "").trim());
			int birthDay = Integer.parseInt(var23.substring(24, 26).replace("\u0000", "").trim());
			Calendar cl = Calendar.getInstance();
			cl.clear();
			cl.set(birthYeah, birthMonth - 1, birthDay);
			Date BirthDay = cl.getTime();
			ici.setBirthDay(BirthDay);
			String addr = var23.substring(26, 61).replace("\u0000", "").trim();
			ici.setAddr(addr);
			String id = var23.substring(61, 79).replace("\u0000", "").trim();
			ici.setIDCard(id);
			String dep = var23.substring(79, 94).replace("\u0000", "").trim();
			ici.setDepartment(dep);
			String sDateYeah = var23.substring(94, 98).replace("\u0000", "").trim();
			String sDateMonth = var23.substring(98, 100).replace("\u0000", "").trim();
			String sDateDay = var23.substring(100, 102).replace("\u0000", "").trim();
			String StrartDate = String.format("%s.%s.%s", sDateYeah, sDateMonth, sDateDay);
			ici.setStrartDate(StrartDate);
			String eDateYeah = var23.substring(102, 110).replace("\u0000", "").trim();
			if (eDateYeah.equals("长期")) {
				ici.setEndDate("长期");
			} else {
				eDateYeah = var23.substring(102, 106).replace("\u0000", "").trim();
				String eDateMonth = var23.substring(106, 108).replace("\u0000", "").trim();
				String eDateDay = var23.substring(108, 110).replace("\u0000", "").trim();
				String EndDate = String.format("%s.%s.%s", eDateYeah, eDateMonth, eDateDay);
				ici.setEndDate(EndDate);
			}

		}
	}

	protected static void PersonInfoUtoGHK(byte[] cCardData, HSIDCardInfo ici) throws Exception {
		String var23 = new String(cCardData, "UTF-16LE");
		String Name = var23.substring(0, 15).replace("\u0000", "").trim();
		ici.setPeopleName(Name);
		String Sex = var23.substring(15, 16).replace("\u0000", "").trim();
		ici.setSex(Sex.equals("1") ? "男" : "女");
		int birthYeah = Integer.parseInt(var23.substring(18, 22).replace("\u0000", "").trim());
		int birthMonth = Integer.parseInt(var23.substring(22, 24).replace("\u0000", "").trim());
		int birthDay = Integer.parseInt(var23.substring(24, 26).replace("\u0000", "").trim());
		Calendar cl = Calendar.getInstance();
		cl.clear();
		cl.set(birthYeah, birthMonth - 1, birthDay);
		Date BirthDay = cl.getTime();
		ici.setBirthDay(BirthDay);
		String addr = var23.substring(26, 61).replace("\u0000", "").trim();
		ici.setAddr(addr);
		String id = var23.substring(61, 79).replace("\u0000", "").trim();
		ici.setIDCard(id);
		String dep = var23.substring(79, 94).replace("\u0000", "").trim();
		ici.setDepartment(dep);
		String sDateYeah = var23.substring(94, 98).replace("\u0000", "").trim();
		String sDateMonth = var23.substring(98, 100).replace("\u0000", "").trim();
		String sDateDay = var23.substring(100, 102).replace("\u0000", "").trim();
		String StrartDate = String.format("%s.%s.%s", sDateYeah, sDateMonth, sDateDay);
		ici.setStrartDate(StrartDate);
		String eDateYeah = var23.substring(102, 110).replace("\u0000", "").trim();
		String PassCheckID;
		String issuesNum;
		if (eDateYeah.equals("长期")) {
			ici.setEndDate("长期");
		} else {
			eDateYeah = var23.substring(102, 106).replace("\u0000", "").trim();
			PassCheckID = var23.substring(106, 108).replace("\u0000", "").trim();
			issuesNum = var23.substring(108, 110).replace("\u0000", "").trim();
			String EndDate = String.format("%s.%s.%s", eDateYeah, PassCheckID, issuesNum);
			ici.setEndDate(EndDate);
		}

		PassCheckID = var23.substring(110, 119).replace("\u0000", "").trim();
		ici.setPassCheckID(PassCheckID);
		issuesNum = var23.substring(119, 121).replace("\u0000", "").trim();
		ici.setissuesNum(issuesNum);
	}

	protected static void PersonInfoUtoGForeigners(byte[] cCardData, HSIDCardInfo ici) throws Exception {
		String str = new String(cCardData, "UTF-16LE");
		String Name = str.substring(0, 60).replace("", "").trim();
		ici.setPeopleName(Name);
		String Sex = str.substring(60, 61).replace("", "").trim();
		int d = Integer.parseInt(Sex);
		switch (d) {
		case 0:
			Sex = "未知";
			break;
		case 1:
			Sex = "男";
			break;
		case 2:
			Sex = "女";
			break;
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		default:
			Sex = "不详";
			break;
		case 9:
			Sex = "未说明";
		}

		ici.setSex(Sex);
		String idcard = str.substring(61, 76).replace("", "").trim();
		ici.setIDCard(idcard);
		String cityCode = str.substring(76, 79).replace("", "").trim();
		ici.setstrNationCode(getNationCodes(cityCode));
		String chineseName = str.substring(79, 94).replace("", "").trim();
		ici.setstrChineseName(chineseName);
		String effDate = str.substring(94, 102).replace("", "").trim();
		ici.setStrartDate(effDate);
		String eDateYeah = str.substring(102, 110).replace("\u0000", "").trim();
		if (eDateYeah.equals("长期")) {
			ici.setEndDate("长期");
		} else {
			String expDate = str.substring(102, 110).replace("", "").trim();
			ici.setEndDate(expDate);
		}

		int birthYeah = Integer.parseInt(str.substring(110, 114).replace("\u0000", "").trim());
		int birthMonth = Integer.parseInt(str.substring(114, 116).replace("\u0000", "").trim());
		int birthDay = Integer.parseInt(str.substring(116, 118).replace("\u0000", "").trim());
		Calendar cl = Calendar.getInstance();
		cl.clear();
		cl.set(birthYeah, birthMonth - 1, birthDay);
		Date BirthDay = cl.getTime();
		ici.setBirthDay(BirthDay);
		String cerVer = str.substring(118, 120).replace("", "").trim();
		ici.setstrCertVer(cerVer);
		String cerorg = str.substring(120, 124).replace("", "").trim();
		ici.setDepartment(cerorg);
	}

	private static String getNationCodes(String str) {
		if (str.equals("AFG")) {
			return "阿富汗";
		} else if (str.equals("ALB")) {
			return "阿尔巴尼亚";
		} else if (str.equals("DZA")) {
			return "阿尔及利亚";
		} else if (str.equals("ASM")) {
			return "美属萨摩亚";
		} else if (str.equals("AND")) {
			return "安道尔";
		} else if (str.equals("AGO")) {
			return "安哥拉";
		} else if (str.equals("AIA")) {
			return "安圭拉";
		} else if (str.equals("ATA")) {
			return "南极洲";
		} else if (str.equals("ATG")) {
			return "安提瓜和巴布达";
		} else if (str.equals("ARG")) {
			return "阿根廷";
		} else if (str.equals("ARM")) {
			return "亚美尼亚";
		} else if (str.equals("ABW")) {
			return "阿鲁巴";
		} else if (str.equals("AUS")) {
			return "澳大利亚";
		} else if (str.equals("AUT")) {
			return "奥地利";
		} else if (str.equals("AZE")) {
			return "阿塞拜疆";
		} else if (str.equals("BHS")) {
			return "巴哈马";
		} else if (str.equals("BHR")) {
			return "巴林";
		} else if (str.equals("BGD")) {
			return "孟加拉国";
		} else if (str.equals("BRB")) {
			return "巴巴多斯";
		} else if (str.equals("BLR")) {
			return "白俄罗斯";
		} else if (str.equals("BEL")) {
			return "比利时";
		} else if (str.equals("BLZ")) {
			return "伯利兹";
		} else if (str.equals("BEN")) {
			return "贝宁";
		} else if (str.equals("BMU")) {
			return "百慕大";
		} else if (str.equals("BTN")) {
			return "不丹";
		} else if (str.equals("BOL")) {
			return "玻利维亚";
		} else if (str.equals("BIH")) {
			return "波黑";
		} else if (str.equals("BWA")) {
			return "博茨瓦纳";
		} else if (str.equals("BVT")) {
			return "布维岛";
		} else if (str.equals("BRA")) {
			return "巴西";
		} else if (str.equals("IOT")) {
			return "英属印度洋领土";
		} else if (str.equals("BRN")) {
			return "文莱";
		} else if (str.equals("BGR")) {
			return "保加利亚";
		} else if (str.equals("BFA")) {
			return "布基纳法索";
		} else if (str.equals("BDI")) {
			return "布隆迪";
		} else if (str.equals("KHM")) {
			return "柬埔寨";
		} else if (str.equals("CMR")) {
			return "喀麦隆";
		} else if (str.equals("CAN")) {
			return "加拿大";
		} else if (str.equals("CPV")) {
			return "佛得角";
		} else if (str.equals("CYM")) {
			return "开曼群岛";
		} else if (str.equals("CAF")) {
			return "中非";
		} else if (str.equals("TCD")) {
			return "乍得";
		} else if (str.equals("CHL")) {
			return "智利";
		} else if (str.equals("CHN")) {
			return "中国";
		} else if (str.equals("HKG")) {
			return "香港";
		} else if (str.equals("MAC")) {
			return "澳门";
		} else if (str.equals("TWN")) {
			return "台湾";
		} else if (str.equals("CSR")) {
			return "圣诞岛";
		} else if (str.equals("CCK")) {
			return "科科斯(基林)群岛";
		} else if (str.equals("COL")) {
			return "哥伦比亚";
		} else if (str.equals("COM")) {
			return "科摩罗";
		} else if (str.equals("COG")) {
			return "刚果（布）";
		} else if (str.equals("COD")) {
			return "刚果（金）";
		} else if (str.equals("COK")) {
			return "库克群岛";
		} else if (str.equals("CRI")) {
			return "哥斯达黎加";
		} else if (str.equals("CIV")) {
			return "科特迪瓦";
		} else if (str.equals("HRV")) {
			return "克罗地亚";
		} else if (str.equals("CUB")) {
			return "古巴";
		} else if (str.equals("CYP")) {
			return "塞浦路斯";
		} else if (str.equals("CZE")) {
			return "捷克";
		} else if (str.equals("DNK")) {
			return "丹麦";
		} else if (str.equals("DJI")) {
			return "吉布提";
		} else if (str.equals("DMA")) {
			return "多米尼克";
		} else if (str.equals("DOM")) {
			return "多米尼加共和国";
		} else if (str.equals("TMP")) {
			return "东帝汶";
		} else if (str.equals("ECU")) {
			return "厄瓜多尔";
		} else if (str.equals("EGY")) {
			return "埃及";
		} else if (str.equals("SLV")) {
			return "萨尔瓦多";
		} else if (str.equals("GNQ")) {
			return "赤道几内亚";
		} else if (str.equals("ERI")) {
			return "厄立特里亚";
		} else if (str.equals("EST")) {
			return "爱沙尼亚";
		} else if (str.equals("ETH")) {
			return "埃塞俄比亚";
		} else if (str.equals("FLK")) {
			return "福克兰群岛(马尔维纳斯)";
		} else if (str.equals("FRO")) {
			return "法罗群岛";
		} else if (str.equals("FJI")) {
			return "斐济";
		} else if (str.equals("FIN")) {
			return "芬兰";
		} else if (str.equals("FRA")) {
			return "法国";
		} else if (str.equals("GUF")) {
			return "法属圭亚那";
		} else if (str.equals("PYF")) {
			return "法属波利尼西亚";
		} else if (str.equals("ATF")) {
			return "法属南部领土";
		} else if (str.equals("GAB")) {
			return "加蓬";
		} else if (str.equals("GMB")) {
			return "冈比亚";
		} else if (str.equals("GEO")) {
			return "格鲁吉亚";
		} else if (str.equals("DEU")) {
			return "德国";
		} else if (str.equals("GHA")) {
			return "加纳";
		} else if (str.equals("GIB")) {
			return "直布罗陀";
		} else if (str.equals("GRC")) {
			return "希腊";
		} else if (str.equals("GRL")) {
			return "格陵兰";
		} else if (str.equals("GRD")) {
			return "格林纳达";
		} else if (str.equals("GLP")) {
			return "瓜德罗普";
		} else if (str.equals("GUM")) {
			return "关岛";
		} else if (str.equals("GTM")) {
			return "危地马拉";
		} else if (str.equals("GIN")) {
			return "几内亚";
		} else {
			return str.equals("GNB") ? "几内亚比绍" : "";
		}
	}

	public static void copy(Context context, String fileName, String saveName, String savePath) {
		File path = new File(savePath);
		if (!path.exists()) {
			path.mkdir();
		}

		try {
			File var11 = new File(savePath + "/" + saveName);
			if (var11.exists() && var11.length() > 0L) {
				Log.i("LU", saveName + "存在了");
				return;
			}

			FileOutputStream fos = new FileOutputStream(var11);
			InputStream inputStream = context.getResources().getAssets().open(fileName);
			byte[] buf = new byte[1024];
			boolean var10 = false;

			int len1;
			while ((len1 = inputStream.read(buf)) != -1) {
				fos.write(buf, 0, len1);
			}

			fos.close();
			inputStream.close();
			Log.i("HSBlueApi", "copy: 解码库复制成功");
		} catch (Exception var12) {
			Log.i("LU", "IO异常");
		}

	}

	/**
	 * byLicData 1、可为空(Hs_Cmd.byLicData) libwltdecode.so库解码 2、照片头像bmp数据
	 * libHSRAW2BMP.so与libwlt2bmp.so库解码
	 */
	private static final byte[] byLicData = new byte[] { 5, 0, 1, 0, 91, 3, 51, 1, 90, -77, 30, 0 };

	/**
	 * 提供身份证照片解码方法
	 * 
	 * @param filepath 文件路径(用于存放资源文件或照片头像)
	 * @param wltdata  从身份证读取的1024字节照片数据
	 * @return 照片的Bitmap对象
	 */
	public static Bitmap unpackPic(String filepath, byte[] wltdata) {
		int ret = -1;
		/* ret 返回值:1：解码成功 -2：加载照片解码库函数失败-1：加载照片解码库失败 其它值：一所照片解码库返回的错误值，未知含义 */
		Bitmap bitmap = null;
		try {
			/* 平均耗时21毫秒 */
			byte[] BGRbuf = new byte[38556];
			/* 返回值为1表示头像解码成功 */
			ret = RAW2BMP.HS_RAW2BMP(wltdata, BGRbuf, 602);
			byte[] bmpBuf = new byte[102 * 126 * 3 + 54 + 126 * 2]; // 照片头像bmp数据
			String path = filepath + "/zp2.bmp";
			RAW2BMP.BGR2BMP(BGRbuf, bmpBuf, path);
			bitmap = BitmapFactory.decodeByteArray(bmpBuf, 0, bmpBuf.length);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (1 != ret) {
				System.out.println("hwwface  " + " libHSRAW2BMP.so与libwlt2bmp.so库解码  失败");
			}
		}

		if (1 != ret) {
			/* 上一个解码失败启动下一个 */
			try {
				/* 平均耗时34毫秒 */
				ret = com.ivsign.android.IDCReader.IDCReaderSDK.wltInit(filepath);
				if (ret == 0) {
					/* 返回值为1表示头像解码成功 */
					ret = com.ivsign.android.IDCReader.IDCReaderSDK.wltGetBMP(wltdata, byLicData);
					if (1 == ret) {
						/* 文件名只能为zp.bmp native已经设置了 */
						String path = filepath + "/zp.bmp";
						FileInputStream fis = new FileInputStream(path);
						bitmap = BitmapFactory.decodeStream(fis);
						fis.close();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (1 != ret) {
					System.out.println("hwwface " + "单独  libwltdecode.so库解码 失败");
				}
			}
		}
		return bitmap;
	}

	public static void showToast(final Activity ctx, final String str) {
		if (Thread.currentThread().getName().equals("main")) {
			Toast.makeText(ctx, str, 0).show();
		} else {
			ctx.runOnUiThread(new Runnable() {
				public void run() {
					Toast.makeText(ctx, str, 0).show();
				}
			});
		}

	}
}
